//
//  CropViewController.m
//  AnXin100
//
//  Created by Sakura on 2018/11/12.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "CropViewController.h"

#import "CropModel.h"

@interface CropViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *mainTable;
@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) NSArray *tableAry;
@property (nonatomic, strong) NSArray *tableAry1;
@property (nonatomic, strong) NSArray *tableAry2;
@property (nonatomic, strong) NSArray *contentAry;

/** 新加的 左边的数组 */
@property (nonatomic , strong) NSMutableArray *leftArray;

@end

@implementation CropViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:self.mainTable];
    [self.view addSubview:self.mainScrollView];
    /** 调用二级列表的接口 */
    [self requestSecondLevelList];
    

   _tableAry = @[@"基础知识", @"形态特征", @"生长环境", @"分布范围", @"繁殖方法", @"栽培品种"];
    _tableAry1 = @[@"基础知识", @"形态特征", @"生长环境", @"分布范围", @"繁殖方法", @"栽培品种"];
    [self.view addSubview:self.mainTable];
    [self.view addSubview:self.mainScrollView];
    _tableAry2 = @[@"病害", @"草害", @"虫害"];

    
}

/** 创建tableView  */
- (UITableView *)mainTable {
    
    if (!_mainTable) {
        _mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH / 4, SCREEN_HEIGHT - Hn - 150 - 44)];
        _mainTable.delegate = self;
        _mainTable.dataSource = self;
        [_mainTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
        _mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _mainTable;
}

/** 创建scrollView  */
- (UIScrollView *)mainScrollView {
    
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 4, 0, SCREEN_WIDTH / 4 * 3, SCREEN_HEIGHT - Hn - 150 - 44)];
        _mainScrollView.backgroundColor = [UIColor whiteColor];
    }
    return _mainScrollView;
}

/** 分区里面的row的个数 */

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return self.leftArray.count;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    CropModel *model = self.leftArray[indexPath.row];
    cell.textLabel.text = model.columnName;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    

    [_mainScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    if (_isShow) {
        NSDictionary *dic = _contentAry[indexPath.row];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, _mainScrollView.mj_w - 20, 30)];
        titleLabel.text = [dic allKeys][0];
        titleLabel.textColor = COLOR_GREEN;
        titleLabel.font = [UIFont systemFontOfSize:16];
        [_mainScrollView addSubview:titleLabel];
        
        if (indexPath.row < _tableAry1.count - 1) {
            
            UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, _mainScrollView.mj_w - 20, 0)];
            contentLabel.numberOfLines = 0;
            contentLabel.font = [UIFont systemFontOfSize:14];
            contentLabel.textColor = COLOR_DIV;
            contentLabel.text = [dic allValues][0];
            [contentLabel sizeToFit];
            _mainScrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(contentLabel.frame) + 10);
            [_mainScrollView addSubview:contentLabel];
            
        } else {
            NSArray *array = @[@"巨峰", @"阳光玫瑰", @"美人指",@"巨峰", @"阳光玫瑰", @"美人指",@"巨峰", @"阳光玫瑰", @"美人指"];
            CGFloat y = 50;
            CGFloat a = 0;
            for (NSInteger i = 0 ; i < array.count; i++) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                btn.frame = CGRectMake(20 * (i % 3 + 1) + (_mainScrollView.mj_w - 80) / 3 * (i % 3), y, (_mainScrollView.mj_w - 80) / 3, 25);
                y = (25 + 10) * ((i + 1) / 3) + 50;
                [btn setTitle:array[i] forState:UIControlStateNormal];
                btn.backgroundColor = COLOR_GREEN;
                btn.titleLabel.font = FONT_FOURTEEN;
                [_mainScrollView addSubview:btn];
                a = CGRectGetMaxY(btn.frame);
            }
            _mainScrollView.contentSize = CGSizeMake(0, y + 10);
            
        }
    } else {
        
        NSArray *array = @[@"黑腐病", @"煤点病", @"拟茎点霉腐烂病",@"白腐病", @"穗轴褐枯病", @"炭疽病",@"房枯病", @"大房枯病", @"黑痘病", @"苦腐病", @"霜霉病", @"灰霉病"];
        CGFloat y = 10;
        CGFloat a = 0;
        for (NSInteger i = 0 ; i < array.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(20 * (i % 3 + 1) + (_mainScrollView.mj_w - 80) / 3 * (i % 3), y, (_mainScrollView.mj_w - 80) / 3, 25);
            y = (25 + 10) * ((i + 1) / 3) + 10;
            [btn setTitle:array[i] forState:UIControlStateNormal];
            btn.titleLabel.font = FONT_FOURTEEN;
            [btn setTitleColor:COLOR_Gray forState:UIControlStateNormal];
            btn.layer.cornerRadius =  3;
            btn.layer.borderColor = COLOR_Gray.CGColor;
            btn.layer.borderWidth = 1;
            [_mainScrollView addSubview:btn];
            a = CGRectGetMaxY(btn.frame);
        }
        _mainScrollView.contentSize = CGSizeMake(0, y + 10);
    }
}


#pragma mark - requestNetwork

/** 请求二级列表 */
- (void)requestSecondLevelList{
   
   ZX_WEAK_SELF
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"cropColumnBase"] = @"jj";
  
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircle,  URL_AgricultureCircle_findColumnByBase) success:^(id respose) {
        
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            weakSelf.leftArray = [CropModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"columnList"]];
            
            [self.mainTable reloadData];
            
            if (self.leftArray.count > 0) {
                
                [self.mainTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
            }
            
            
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
        }
        
        [self.mainTable.mj_header endRefreshing];
        [self.mainTable.mj_footer endRefreshing];
        
        
//        LeftTableViewModel *model = self.dataSource[0];
//        [self getCollectionData:model.classificationId];
        
    } failure:^(NSError *error) {
        
        [self.mainTable.mj_header endRefreshing];
        [self.mainTable.mj_footer endRefreshing];
        
    }];
    
    
}





#pragma mark - setter & getter



- (NSMutableArray *)leftArray
{
    if (!_leftArray)
    {
        _leftArray = [NSMutableArray array];
    }
    return _leftArray;
}



@end
